<?php
session_start();
/* 取得当前所在的根目录 */
define('IN_ELS', true);
define('Z_PATH', str_replace('lib/apilogin/qqlogin/oauth/get_access_token.php', '', str_replace('\\', '/', __FILE__)));
require_once(Z_PATH."lib/db.class.php");
require_once(Z_PATH."lib/apilogin/qqlogin/comm/utils.php");

$_SESSION['from_url'] = 'qq';
//获取QQ用户信息
$qq_info = get_qq_info($_SESSION["appid"], $_SESSION["access_token"],$_SESSION["openid"]);

$qq_info = json_decode($qq_info,1);
//$qq_info['figureurl'] = str_replace("?oauth_consumer_key", "", $qq_info['figureurl']);
//$qq_info['figureurl_1'] = str_replace("?oauth_consumer_key", "", $qq_info['figureurl_1']);
//$qq_info['figureurl_2'] = str_replace("?oauth_consumer_key", "", $qq_info['figureurl_2']);
////返回信息处理
//$qq_infoss = get_qqweibo_info($_SESSION["appid"], $_SESSION["access_token"],$_SESSION["openid"]);
//$qq_infoss = str_replace("xml?oauth_consumer_key=(", "", $qq_infoss);
//$qq_infoss = str_replace(")", "", $qq_infoss);
//$qq_infos = json_decode($qq_infoss,true);
//$_SESSION['qq_weibo_name'] = $qq_infos[data][name];

bind_to_openid($_SESSION["openid"],$qq_info);



/*第三方处理用户绑定逻辑
将openid与第三方的帐号做关联
*/
function bind_to_openid($openid,$qq_info)
{
	define('ELS_DATADIR', Z_PATH.'data/');
	require ELS_DATADIR.'config.inc.php';
	$db = new db();	
	$db->connect(UC_DBHOST, UC_DBUSER, UC_DBPW, UC_DBNAME, UC_DBCHARSET, UC_DBCONNECT, UC_DBTABLEPRE);
	
	$qq_user_id	=	"QQ#".$openid;
	$sql = " select id from gnw_user where ex_biaoshi='".$qq_user_id."' ";
	
	$user_info = $db->fetch_first($sql);
	//如果QQ用户已存在，直接登录
	if($user_info['id']>0)
	{
		login_user($user_info['id']);
	}
	//如果不存在，注册新用户
	else
	{
		add_qq_user($openid,$qq_info);
	}
}

/*
 * 添加用户
*/
function add_qq_user($openid, $qq_info)
{
	define('ELS_DATADIR', Z_PATH.'data/');
	define('ELS_ROOT', Z_PATH);
	require ELS_DATADIR.'config.inc.php';
	require_once(Z_PATH.'model/base.php');
	$dbs = new base();	
	$now_time = time();
	$rands = rand(111111,999999);
	$data['user_name']   = "gnw_".$now_time.$rands."_qq";
	$data['password']    = md5("gnw_".$now_time.$rands."_qq");
	$data['nick_name']   = $qq_info['nickname'];
	$data['user_type']   = 2;
	$data['ex_biaoshi']  = "QQ#".$openid;
	$data['ex_source']   = "QQ";
	$data['last_ip']	 = $dbs -> getonlineip();
	$data['last_time']	 = $dbs -> getCurrTimeLine();
	$data['create_time'] = $dbs -> getCurrTimeLine();
	
	$retValue = $dbs->dbInsert("gnw_user",$data);
	$id = $dbs->db->insert_id();
	if($id){
		// 注册成功
		$_SESSION["mid"]			= $id;
		$_SESSION["user_name"]		= $data['user_name'];
	}
	$qqbackurl = "http://local.iknowy.com/";
	header ("Location: ".$qqbackurl);
	exit;
}


/*
 * 登陆用户
*/
function login_user($user_id){

//	$sql = "UPDATE ecs_users SET qq_wb_name = '".$_SESSION['qq_weibo_name']."',head_img='".$qq_info['figureurl_2']."',sex = '".$sex."',ex_user_name='".$qq_info['nickname']."', last_time = '".date("Y-m-d H:i:s")."'  WHERE  user_id =".$user_id." LIMIT  1 ";
//	$GLOBALS['db'] -> Query($sql);

	define('ELS_DATADIR', Z_PATH.'data/');
	define('ELS_ROOT', Z_PATH);
	require ELS_DATADIR.'config.inc.php';
	require_once(Z_PATH.'model/base.php');
	$dbs = new base();	
	
	$sql = " select id,true_name,nick_name from gnw_user where id='".$user_id."' ";
	
	$user_info = $dbs->db->fetch_first($sql);
	
	if($user_info){
		$dbs->db->query("UPDATE gnw_user SET last_ip = '".$dbs -> getonlineip()."',last_time='".$dbs -> getCurrTimeLine()."' WHERE id='$user_id'");
		// 注册成功
		$_SESSION["mid"]			= $user_info['id'];
		$_SESSION["nick_name"]		= $user_info['nick_name'];
		$_SESSION["true_name"]		= $user_info['true_name'];
	}
	$qqbackurl ="http://local.iknowy.com/";
	header ("Location: ".$qqbackurl);
	exit;
}




/*
*获取QQ用户信息
*/
function get_qq_info($appid,$access_token, $openid)
{
    //get user info 的api接口，不要随便更改!!
    $url    = "https://graph.qq.com/user/get_user_info?access_token=".$access_token."&oauth_consumer_key=".$appid."&openid=".$openid;

    $res	= do_get($url); 
	return $res;
}

/*
*获取QQweibo信息
*/
function get_qqweibo_info($appid,$access_token, $openid)
{
	$url    = "https://graph.qq.com/user/get_info?access_token=".$access_token."&oauth_consumer_key=".$appid."&openid=".$openid."&format=xml";
	
    //get user info 的api接口，不要随便更改!!
   
    $res	= do_get($url); 
	return $res;
}

?>
